import {request} from '../../request/index.js'
import regeneratorRuntime from '../../lib/runtime/runtime.js'
import { showModal } from '../../utils/asyncWx.js'
Page({
  data: {
    tabs:[
      {
        id:0,
        value:"全部订单",
        isActive:true
      },{
        id:1,
        value:"待付款",
        isActive:false
      },{
        id:2,
        value:"待收货",
        isActive:false
      },{
        id:3,
        value:"退款/货",
        isActive:false
      }
    ]
  },
  handleTabsItemChange(e) {
		// 获取被点击的标签索引
		const {index} = e.detail
		// 修改原数组
		let {tabs} = this.data
		tabs.forEach((v,i)=>i===index?v.isActive=true:v.isActive=false)
		// 赋值到data
		this.setData({
			tabs
		})
  },
  // 获取订单列表数据
  async getOrderList(type){
    const res = await request({url:'/my/orders/all',data:{type}})
    if(res === null){
      showModal({content:"个人微信无法获取token所以无法查询订单"})
    }
    return
  },
  onShow(options){
    // 先判断是否存在token
    const token = wx.getStorageSync("token");
    if (!token){
      wx.navigateTo({
        url: '/pages/auth/index'
      });
      return
    }
    // 获取小程序页面栈数据，以此获得传递过来的参数type
    let pages =  getCurrentPages();
    // 数组中索引最大的哪个就是当前页面的数据
    let currentPage = pages[pages.length-1]
    //获取type
    const {type} = currentPage.options
    this.getOrderList(type)
  },
  // 根据标题来激活所选数据
  changeTitleByIndex(){
    // 修改原数组
    let {tabs} = this.data
    tabs.forEach((v,i)=>{i===index?v.isActive=true:v.isActive=false})
    // 赋值
    this.setData({tabs})
  }
})